package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.AdiminOrderMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.result.PageResult;
import com.sky.service.AdiminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class AdiminOrderServiceImpl implements AdiminOrderService {
    @Autowired
    AdiminOrderMapper adiminOrderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public PageResult OrderSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(),ordersPageQueryDTO.getPageSize());
        Page<OrderVO> page = adiminOrderMapper.orderSearch(ordersPageQueryDTO);

        List<OrderVO> list = page.getResult();
        for (OrderVO orderVO : list) {
            String dishNames = "";
            List<OrderDetail> orderDetails = orderDetailMapper.selectByOrderId(orderVO.getId());
            for (OrderDetail orderDetail : orderDetails) {
                String name = orderDetail.getName();
                dishNames = dishNames + name + " ";
            }
            orderVO.setOrderDishes(dishNames);
        }
        long total = page.getTotal();

        return new PageResult(total,list);
    }

    @Override
    public OrderVO selectById(Long id) {
        Orders order = adiminOrderMapper.getById(id);

        List<OrderDetail> orderDetails = orderDetailMapper.selectByOrderId(id);

        OrderVO orderVO = new OrderVO();

        BeanUtils.copyProperties(order,orderVO);

        orderVO.setOrderDetailList(orderDetails);

        return orderVO;
    }

    @Override
    public OrderStatisticsVO getOrderStatistics() {

        OrderStatisticsVO orderStatisticsVO = adiminOrderMapper.getOrderStatistics();

        return orderStatisticsVO;
    }

    @Override
    public void confirmOrder(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = adiminOrderMapper.getById(ordersConfirmDTO.getId());
        if (orders.getStatus()==2) {

            orders.setStatus(3);
            orderMapper.update(orders);
        }else {
            throw new OrderBusinessException("订单状态错误,不可接单");

        }    }

    @Override
    public void rejectOrder(OrdersRejectionDTO ordersRejectionDTO) {
        Orders orders = adiminOrderMapper.getById(ordersRejectionDTO.getId());
        if (orders.getStatus()==2) {

            BeanUtils.copyProperties(ordersRejectionDTO,orders);
            orders.setStatus(6);
            orders.setCancelTime(LocalDateTime.now());
            orderMapper.update(orders);
        }else {
            throw new OrderBusinessException("订单状态错误,不可拒单");

        }    }

    @Override
    public void sendOrder(Long id) {
        Orders orders = adiminOrderMapper.getById(id);
        if (orders.getStatus()==3) {
            orders.setStatus(4);
            orderMapper.update(orders);
//            LocalDateTime deliveryTime = LocalDateTime.now();

        }else {
            throw new OrderBusinessException("订单状态错误,不可派单");
        }
    }

    @Override
    public void compelete(Long id) {
        Orders orders = adiminOrderMapper.getById(id);
        if (orders.getStatus()==4) {
            orders.setStatus(5);
            orderMapper.update(orders);
        }else {
            throw new OrderBusinessException("订单状态错误,不可完成");
        }
    }

    @Override
    public void cancelOrder(OrdersCancelDTO ordersCancelDTO) {
        Orders orders = adiminOrderMapper.getById(ordersCancelDTO.getId());
        if (orders.getStatus()==1) {
            BeanUtils.copyProperties(ordersCancelDTO,orders);
            orders.setStatus(6);
            orders.setCancelTime(LocalDateTime.now());
            orderMapper.update(orders);
        }else {
            throw new OrderBusinessException("订单状态错误,不可取消");
        }
    }

}
